package com.glorysoft.tonglingxjpda.ui.order.view


import android.os.Bundle
import android.view.View
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.databinding.ActivityOrderListBinding
import com.glorysoft.tonglingxjpda.ui.order.viewmodel.OrderListViewModel
import com.pda.platform.ui.ui_pdaplatform.datepicker.CustomDatePicker
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import kotlinx.android.synthetic.main.activity_order_list.*
import kotlinx.android.synthetic.main.activity_title.*

/**
 * FileName: OrderListActivity
 * Author: XJH
 * Date: 2021/7/12 17:49
 * Description:
 * History:
 */
class OrderListActivity : BaseActivity<OrderListViewModel, ActivityOrderListBinding>(),
    View.OnClickListener {
    private var mDatePicker: CustomDatePicker? = null  //日期选择器
    private var dateType = 0
    private var beginDate = System.currentTimeMillis()
    private var endDate = System.currentTimeMillis()

    override fun layoutId(): Int {
        return R.layout.activity_order_list
    }

    override fun initVariableId(): Int {
        return BR.orderListViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        rvOrderList.setEmptyView(orderEmptyView)
        tvTitle.text = "订单列表"
        tvEdit.setOnClickListener(this)
        tvSearch.setOnClickListener(this)
        tvCreate.setOnClickListener(this)
//        flCreateTime.setOnClickListener(this)
        flOutTime.setOnClickListener(this)
        viewModel.init()
        tvCreateTime.text = viewModel.getStringDate()
//        tvOutTime.text =  viewModel.getStringDate()
    }

    override fun initData() {
        val beginTimestamp = FreeApi_DateUtils.str2Long("1992-03-20", false)
        val endTimestamp = FreeApi_DateUtils.str2Long("2222-02-22", false)
        mDatePicker = CustomDatePicker(this, { timestamp ->
            val date = FreeApi_DateUtils.longToString(timestamp, "yyyy-MM-dd")
            if (dateType == 1) {
                beginDate = timestamp
                tvCreateTime.text = date

            } else {
                endDate = timestamp
                tvOutTime.text = date
//                if (tvCreateTime.text.isEmpty()) {
//                    endDate = timestamp
//                    tvOutTime.text = date
//                } else {
//                    if (viewModel.compareDate(tvCreateTime.text.toString(), date)) {
//                        endDate = timestamp
//                        tvOutTime.text = date
//                    }
//                }
            }

        }, beginTimestamp, endTimestamp)
        mDatePicker!!.setCancelable(true)
        mDatePicker!!.setCanShowPreciseTime(false)
        mDatePicker!!.setScrollLoop(false)
        mDatePicker!!.setCanShowAnim(false)

        viewModel.defUI.startActivity.observe(this) {
            startActivity(OrderMangerActivity::class.java, it)
//            finish()
        }

    }

    override fun onRestart() {
        super.onRestart()
//        viewModel.searchOrder(tvCreateTime.text.toString(), tvOutTime.text.toString())
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvEdit -> {
                if (llSearchLayout.visibility == View.GONE) {
                    llSearchLayout.visibility = View.VISIBLE
                    tvEdit.text = "隐藏查询数据"
                } else {
                    llSearchLayout.visibility = View.GONE
                    tvEdit.text = "显示查询数据"
                }
            }
            R.id.tvSearch -> {
                if (tvCreateTime.text.toString().isEmpty() && tvOutTime.text.toString().isEmpty()
                    && viewModel.orderInfoEntity.value?.DOCID!!.isEmpty()
                    && viewModel.orderInfoEntity.value?.RESERVED3!!.isEmpty()
                    && viewModel.orderInfoEntity.value?.CUSTOMERCODE!!.isEmpty()
                    && viewModel.orderInfoEntity.value?.GRADE1!!.isEmpty()
                    && viewModel.orderInfoEntity.value?.RESERVED1!!.isEmpty()
                    && viewModel.orderInfoEntity.value?.RESERVED6!!.isEmpty()
                        ) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请输入查询信息", false)
                    return
                }
                viewModel.searchOrder(tvCreateTime.text.toString(), tvOutTime.text.toString())

            }
            R.id.tvCreate -> {
                startActivity(OrderMangerActivity::class.java)
                finish()
            }
            R.id.flCreateTime -> {
                dateType = 1
                mDatePicker!!.show(beginDate)
            }
            R.id.flOutTime -> {
                dateType = 2
                mDatePicker!!.show(endDate)
            }
        }
    }
}